package com.wondream.myframework.app.utils;

public class HyBase64 {
    public static final char[] intToBase64 = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_'};
    public static final byte[] base64ToInt = new byte[]{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};

    public HyBase64() {
    }

    public static String encode(byte[] byteArray) {
        int aLen = byteArray.length;
        int numFullGroups = aLen / 3;
        int numBytesInPartialGroup = aLen - 3 * numFullGroups;
        int resultLen = 4 * ((aLen + 2) / 3);
        StringBuffer result = new StringBuffer(resultLen);
        int inCursor = 0;

        int byte0;
        int byte1;
        for(byte0 = 0; byte0 < numFullGroups; ++byte0) {
            byte1 = byteArray[inCursor++] & 255;
//            int byte1 = byteArray[inCursor++] & 255;
            int byte2 = byteArray[inCursor++] & 255;
            result.append(intToBase64[byte1 >> 2]);
            result.append(intToBase64[byte1 << 4 & 63 | byte1 >> 4]);
            result.append(intToBase64[byte1 << 2 & 63 | byte2 >> 6]);
            result.append(intToBase64[byte2 & 63]);
        }

        if (numBytesInPartialGroup != 0) {
            byte0 = byteArray[inCursor++] & 255;
            result.append(intToBase64[byte0 >> 2]);
            if (numBytesInPartialGroup == 1) {
                result.append(intToBase64[byte0 << 4 & 63]);
                result.append("==");
            } else {
                byte1 = byteArray[inCursor++] & 255;
                result.append(intToBase64[byte0 << 4 & 63 | byte1 >> 4]);
                result.append(intToBase64[byte1 << 2 & 63]);
                result.append('=');
            }
        }

        return result.toString();
    }

    public static byte[] decode(String str) throws Exception {
        int sLen = str.length();
        int numGroups = sLen / 4;
        if (4 * numGroups != sLen) {
            throw new IllegalArgumentException("字串长度必须是4的倍数");
        } else {
            int missingBytesInLastGroup = 0;
            int numFullGroups = numGroups;
            if (sLen != 0) {
                if (str.charAt(sLen - 1) == '=') {
                    ++missingBytesInLastGroup;
                    numFullGroups = numGroups - 1;
                }

                if (str.charAt(sLen - 2) == '=') {
                    ++missingBytesInLastGroup;
                }
            }

            byte[] result = new byte[3 * numGroups - missingBytesInLastGroup];

            try {
                int inCursor = 0;
                int outCursor = 0;

                int ch0;
                int ch1;
                int ch2;
                for(ch0 = 0; ch0 < numFullGroups; ++ch0) {
                    ch1 = base64toInt(str.charAt(inCursor++), base64ToInt);
                    ch2 = base64toInt(str.charAt(inCursor++), base64ToInt);
//                    int ch2 = base64toInt(str.charAt(inCursor++), base64ToInt);
                    int ch3 = base64toInt(str.charAt(inCursor++), base64ToInt);
                    result[outCursor++] = (byte)(ch1 << 2 | ch2 >> 4);
                    result[outCursor++] = (byte)(ch2 << 4 | ch2 >> 2);
                    result[outCursor++] = (byte)(ch2 << 6 | ch3);
                }

                if (missingBytesInLastGroup != 0) {
                    ch0 = base64toInt(str.charAt(inCursor++), base64ToInt);
                    ch1 = base64toInt(str.charAt(inCursor++), base64ToInt);
                    result[outCursor++] = (byte)(ch0 << 2 | ch1 >> 4);
                    if (missingBytesInLastGroup == 1) {
                        ch2 = base64toInt(str.charAt(inCursor++), base64ToInt);
                        result[outCursor++] = (byte)(ch1 << 4 | ch2 >> 2);
                    }
                }

                return result;
            } catch (Exception var13) {
                throw var13;
            }
        }
    }

    private static int base64toInt(char c, byte[] alphaToInt) throws Exception {
        int result = alphaToInt[c];
        if (result < 0) {
            throw new Exception("非法索引值");
        } else {
            return result;
        }
    }
}